package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.ResultSet;
import java.sql.*;
import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * this class Generates activity report once a day 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */

public class MessageActivityReport implements Runnable {

	private ConnectionToClient client;
	private int numofactive;
	private int numoflock;
	private int numoffreeze;
	private int numoflend;
	private int numoflate;
	private int row1;
	private static final int oneday=86400000;
	private ArrayList<Object> result= new ArrayList<Object>();
/**
 * constractor of this class 
 */
	public MessageActivityReport() {
		// TODO Auto-generated constructor stub
	this.client= client;
	}
	/**
	 * get the activitiy report from the DB 
	 * @throws SQLException
	 * @throws IOException
	 */
	


	
	public void ActivityReport() throws SQLException, IOException
	
	{
		
	
	
		
		SQLConnection sc = new SQLConnection();
		PreparedStatement active = sc.getConnection().prepareStatement("SELECT COUNT(ID) FROM subscriber WHERE status=0;");
		PreparedStatement lock = sc.getConnection().prepareStatement("SELECT COUNT(ID) FROM subscriber WHERE status=1;");
		PreparedStatement freeze = sc.getConnection().prepareStatement("SELECT COUNT(ID) FROM subscriber WHERE status=2;");
		PreparedStatement lend = sc.getConnection().prepareStatement("SELECT COUNT(LendId) FROM db1.lending WHERE Lend='yes';");
		PreparedStatement notintime = sc.getConnection().prepareStatement("SELECT COUNT(LendId) FROM db1.lending WHERE (ActualReturnDate- EndDate > 0);");
		PreparedStatement resultac = sc.getConnection().prepareStatement("INSERT INTO db1.activity (idactivity,active,locked,freeze,lend,notintime,date) VALUES(?,?,?,?,?,?,?);");
		PreparedStatement numoflines = sc.getConnection().prepareStatement("SELECT * FROM db1.activity;");

		sc.getConnection().setAutoCommit(true);
		
		java.sql.ResultSet rsactive =  active.executeQuery();
		rsactive.next();
		setNumofactive(rsactive.getInt(1));
		
		java.sql.ResultSet rslock =  lock.executeQuery();
		rslock.next();
		setNumoflock(rslock.getInt(1));
		
		java.sql.ResultSet rsfreeze =  freeze.executeQuery();
		rsfreeze.next();
		setNumoflate(rsfreeze.getInt(1));
		
		java.sql.ResultSet rslend =  lend.executeQuery();
		rslend.next();
		setNumoflend(rslend.getInt(1));
		
		java.sql.ResultSet rsnotintime =  notintime.executeQuery();
		rsnotintime.next();
		setNumoflate(rsnotintime.getInt(1));
		
		java.util.Date today = new java.util.Date();
		java.sql.Date timestamp = new java.sql.Date(today.getTime());
		
		
		java.sql.ResultSet rs= numoflines.executeQuery();
		
		/*	while (rs.next())
			{
				
				result.add("Activity");
				result.add(getNumofactive());
				result.add(getNumoflock());
				result.add(getNumoffreeze());
				result.add(getNumoflend());
				result.add(getNumoflate());
				result.add(timestamp);
		 
			}
				
		client.sendToClient(result);
		*/
		
	//	rs.first();
		
		row1 = 1;
		while(rs.next()){
			int temp = rs.getInt(1);
				if(row1>temp)
					row1=row1;
					else{
					row1=temp;
					}
		
		}
		
		resultac.setInt(1, this.row1+1);
		resultac.setInt(2, getNumofactive());
		resultac.setInt(3, getNumoflock());
		resultac.setInt(4, getNumoffreeze());
		resultac.setInt(5, getNumoflend());
		resultac.setInt(6, getNumoflate());
		resultac.setDate(7,timestamp);
		
		resultac.executeUpdate();
		
		/*try 
		{
			Thread.sleep(10);
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}*/
	
		
		
	}
	
/**
 * 
 * @return number of lended books
 * @throws SQLException
 * @throws IOException
 */
	public  int getlend() throws SQLException, IOException
	


	{
	SQLConnection sc = new SQLConnection();
	PreparedStatement lend = sc.getConnection().prepareStatement("SELECT count(distinct(copyid)) FROM db1.lending ;");
	java.sql.ResultSet rslend =  lend.executeQuery();
	rslend.next();
	return(rslend.getInt(1));
	}
	
	
	
	
	
	
	/**
	 * 
	 * @return the number of active users 
	 */
	public int getNumofactive() {
		return numofactive;
	}

/**
 * 
 * @param get the number of active users 
 */
	public void setNumofactive(int numofactive) {
		this.numofactive = numofactive;
	}
/**
 * 
 * @return the number of lock users 
 */
	public int getNumoflock() {
		return numoflock;
	}

/**
 * 
 * @param gets the number of lock users 
 */
	public void setNumoflock(int numoflock) {
		this.numoflock = numoflock;
	}

/**
 * 
 * @return the numbers of freezed users 
 */
	public int getNumoffreeze() {
		return numoffreeze;
	}

/**
 * 
 * @param gets the number of freezed users 
 */
	public void setNumoffreeze(int numoffreeze) {
		this.numoffreeze = numoffreeze;
	}
/**
 * 
 * @return the number of lended books 
 */

	public int getNumoflend() {
		return numoflend;
	}

/**
 * 
 * @param gets the number of lended books
 */
	public void setNumoflend(int numoflend) {
		this.numoflend = numoflend;
	}

/**
 * 
 * @return the number of late of returnd 
 */
	public int getNumoflate() {
		return numoflate;
	}

/**
 * 
 * @param gets the number of late in return 
 */
	public void setNumoflate(int numoflate) {
		this.numoflate = numoflate;
	}


@Override
public void run() {
	
		try {
			ActivityReport();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

}

	
	
}
